<template>
  <el-breadcrumb class="breadcrumb" separator="/">
    <el-breadcrumb-item v-for="(item, index) in breadArr" :key="item.path">
      <!-- 不可点击项 -->
      <span v-if="index === breadArr.length - 1" class="no-redirect">{{
        generateTitle(item.meta.title)
      }}</span>
      <!-- 可点击项 -->
      <a v-else class="redirect" @click.prevent="onLinkClick(item)">{{
        generateTitle(item.meta.title)
      }}</a>
    </el-breadcrumb-item>
  </el-breadcrumb>
</template>

<script setup>
// 实现动态面包
import { ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { generateTitle } from '@/utils/i18n'

const breadArr = ref([])

const route = useRoute()
const router = useRouter()

// 当路由发生变化的时候，重新计算这个面包屑
watch(
  () => route,
  (newRoute) => {
    // 获取当前路由的匹配信息
    const matched = newRoute.matched
    // 获取当前路由的参数
    // const params = { ...route.params, ...route.query }
    // 创建面包屑数组
    breadArr.value = matched.filter((item) => {
      return item.meta && item.meta.title
    })

    console.log(breadArr.value)
  },
  {
    immediate: true,
    deep: true
  }
)

const onLinkClick = (item) => {
  router.push(item.path)
  // console.log(item)
  // 跳转到对应的路由
  // this.$router.push({
  //   path: item.path,
  //   query: params
  // })
  // this.$router.push({
  //   path: item.path,
  //   query: { ...route.params, ...route.query }
  // })
  // this.$router.push({})
}

// 将来需要进行主题替换，所以这里获取下动态样式
const store = useStore()
// eslint-disable-next-line
const linkHoverColor = ref(store.getters.cssVar.menuBg)
</script>

<style lang="scss" scoped>
.breadcrumb {
  display: inline-block;
  font-size: 14px;
  line-height: 50px;
  margin-left: 8px;

  ::v-deep .no-redirect {
    color: #97a8be;
    cursor: text;
  }

  .redirect {
    color: #666;
    font-weight: 600;
  }

  .redirect:hover {
    // 将来需要进行主题替换，所以这里不去写死样式
    color: v-bind(linkHoverColor);
  }
}
</style>
